#include "simple_rest_api.h"
#include <iostream>
#include <vector>
#include <algorithm>

// 定义User DTO
struct User {
    int id;
    std::string name;
    std::string email;

    // 将User转换为JSON
    nlohmann::json toJson() const {
        return {
            {"id", id},
            {"name", name},
            {"email", email}
        };
    }

    // 从JSON转换为User
    static User fromJson(const nlohmann::json& j) {
        User user;
        user.id = j["id"].get<int>();
        user.name = j["name"].get<std::string>();
        user.email = j["email"].get<std::string>();
        return user;
    }
};

int main() {
    SimpleRest::RestApiHandler api;
    const std::string data_file = "data.json";

    // 初始化数据文件
    nlohmann::json initial_data = {{"users", nlohmann::json::array()}};
    api.saveDataToFile(data_file, initial_data);

    // 使用链式调用添加API路由
    api.addRoute("/users", SimpleRest::HttpMethod::GET, [&api, data_file](const SimpleRest::HttpRequest& request) {
        nlohmann::json data = api.loadDataFromFile(data_file);
        return SimpleRest::HttpResponse{200, data["users"]};
    })
    .addRoute("/users", SimpleRest::HttpMethod::POST, [&api, data_file](const SimpleRest::HttpRequest& request) {
        nlohmann::json data = api.loadDataFromFile(data_file);
        User new_user = User::fromJson(request.body);
        data["users"].push_back(new_user.toJson());
        api.saveDataToFile(data_file, data);
        return SimpleRest::HttpResponse{201, new_user.toJson()};
    })
    .addRoute("/users/<id>", SimpleRest::HttpMethod::GET, [&api, data_file](const SimpleRest::HttpRequest& request) {
        nlohmann::json data = api.loadDataFromFile(data_file);
        int user_id = std::stoi(request.uri.substr(request.uri.find_last_of('/') + 1));
        for (const auto& user : data["users"]) {
            if (user["id"].get<int>() == user_id) {
                return SimpleRest::HttpResponse{200, user};
            }
        }
        return SimpleRest::HttpResponse{404, "User not found"};
    })
    .addRoute("/users/<id>", SimpleRest::HttpMethod::PUT, [&api, data_file](const SimpleRest::HttpRequest& request) {
        nlohmann::json data = api.loadDataFromFile(data_file);
        int user_id = std::stoi(request.uri.substr(request.uri.find_last_of('/') + 1));
        User updated_user = User::fromJson(request.body);
        for (auto& user : data["users"]) {
            if (user["id"].get<int>() == user_id) {
                user = updated_user.toJson();
                api.saveDataToFile(data_file, data);
                return SimpleRest::HttpResponse{200, user};
            }
        }
        return SimpleRest::HttpResponse{404, "User not found"};
    })
    .addRoute("/users/<id>", SimpleRest::HttpMethod::DELETE, [&api, data_file](const SimpleRest::HttpRequest& request) {
        nlohmann::json data = api.loadDataFromFile(data_file);
        int user_id = std::stoi(request.uri.substr(request.uri.find_last_of('/') + 1));
        auto it = data["users"].begin();
        for (; it != data["users"].end(); ++it) {
            if (it->at("id").get<int>() == user_id) { // 使用at()访问键并转换为int
                break;
            }
        }
        if (it != data["users"].end()) {
            data["users"].erase(it);
            api.saveDataToFile(data_file, data);
            return SimpleRest::HttpResponse{200, "User deleted"};
        }
        return SimpleRest::HttpResponse{404, "User not found"};
    });

    // 模拟GET请求获取所有用户
    api.testRoute(SimpleRest::HttpMethod::GET, "/users");

    // 创建新用户
    User new_user{1, "John Doe", "john@example.com"};
    api.testRoute(SimpleRest::HttpMethod::POST, "/users", new_user.toJson());

    // 模拟GET请求获取单个用户
    api.testRoute(SimpleRest::HttpMethod::GET, "/users/1");

    // 更新用户信息
    User updated_user{1, "John Smith", "john.smith@example.com"};
    api.testRoute(SimpleRest::HttpMethod::PUT, "/users/1", updated_user.toJson());

    // 再次模拟GET请求获取更新后的用户
    api.testRoute(SimpleRest::HttpMethod::GET, "/users/1");

    // 删除用户
    api.testRoute(SimpleRest::HttpMethod::DELETE, "/users/1");

    // 再次模拟GET请求获取所有用户
    api.testRoute(SimpleRest::HttpMethod::GET, "/users");

    return 0;
}